<template>
  <div class="app-container table-app-container">
    <div class="fixed-table">
      <div class="fixed-table-actionbar">
        <table-filter :org-code="orgCode" />
        <action-bar v-if="!forSelection" />
      </div>
      <PageableTable
        :loading="loading"
        :data="insideTableData"
        :total="totalElements"
        :page="currentPage"
        :page-size="pageSize"
        :table-height="tableHeight"
        class="fixed-table-table"
        :multiple-selected="multipleSelected"
        @page-change="onPageChange"
        @sort-change="onSortChange"
        @selection-change="onSelectionChange"
      >

        <el-table-column v-for="column in insideColumns" :key="column.key" :label="column.title" :prop="column.key" :sortable="column.sortable">
          <template slot-scope="scope">
            <column-render :render="column.render" :row="scope.row" :column="column" />
          </template>
        </el-table-column>
        <el-table-column v-if="!forSelection" label="" align="center" width="180" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button title="编辑" type="text" size="mini" icon="el-icon-edit" @click="edit(row.id)" />
            <el-button v-if="row.username !== 'admin'" title="删除" type="text" size="mini" icon="el-icon-delete" @click="remove(row.id)" />
            <el-button title="重置密码" type="text" size="mini" icon="el-icon-set-up" @click="resetPassword(row.id)" />
          </template>
        </el-table-column>
      </PageableTable>
    </div>
    <FormDialog
      title="编辑"
      :show.sync="dialogFormVisible"
      @on-ok="ok"
      @on-cancel="cancel"
    >
      <EditForm :id="editId" ref="editForm" />
    </FormDialog>
    <FormDialog
      title="重置密码"
      width="40%"
      :show.sync="dialogResetPasswordFormVisible"
      @on-ok="resetPasswordOk"
      @on-cancel="resetPasswordCancel"
    >
      <ResetPasswordForm :id="editId" ref="resetPasswordForm" />
    </FormDialog>
  </div>
</template>

<script>
// tableHeight属性，从mixin混入而来
// secondary package based on el-pagination
import FormDialog from '@neu/biz-core/components/FormDialog'
// 表格查询组件独立编写，需要传递pageable-table实例的mixin方法filter
import TableFilter from './components/tableFilter'
// 编辑实例组件独立编写，需要传递id属性，判断新建还是更新操作
import EditForm from './components/editForm'
import ResetPasswordForm from './components/resetPasswordForm'
import ActionBar from './components/actionBar'
import PageableTable from '@neu/biz-core/components/PageableTable'

import { page, remove } from '@neu/auth-ui/api/user'
import databind from '@neu/biz-core/mixins/databind'

import { formatDate } from '@neu/biz-core/utils/utils'

export default {
  name: 'UsersPage',
  components: {
    TableFilter,
    FormDialog,
    EditForm,
    ResetPasswordForm,
    ActionBar,
    PageableTable
  },
  mixins: [databind],
  props: {
    multipleSelected: {
      type: Boolean,
      default: false,
      required: false
    },
    orgCode: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      r: page,
      d: remove,
      insideColumns: [
        { title: '姓名', key: 'name', sortable: 'custom' },
        { title: '账号', key: 'username' },
        { title: '电话', key: 'phone' },
        { title: '邮箱', key: 'mail' },
        { title: '创建时间', key: 'createTime', render: (h, params) => { return h('span', formatDate(params.row.createTime)) } }
      ],
      dialogFormVisible: false,
      dialogResetPasswordFormVisible: false,
      dialogStatus: 'create',
      textMap: {
        update: '编辑',
        create: '新建'
      },
      searchObj: {
        s_orgCode: null
      },
      editId: -1
    }
  },
  created() {
    this.searchObj['s_orgCode'] = this.orgCode
    this.filter(this.searchObj)
  },
  methods: {
    // responseFormatter(result) {
    //   return result.filter(it => it.username !== 'admin')
    // },
    edit(id) {
      // 更新当前被编辑实体的ID，自动传递至编辑表单
      this.editId = id
      // 打开对话框
      this.dialogFormVisible = true
    },
    ok() {
      // 直接调用表单实例的保存方法
      this.$refs.editForm.save().then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
        // 调用pageable-table实例的mixin方法refresh()，更新表格
        this.refresh()
        // 关闭对话框
        this.dialogFormVisible = false
      })
    },
    cancel() {
      this.editId = -1
      this.$refs.editForm.reset()
      this.dialogFormVisible = false
    },
    resetPassword(id) {
      this.editId = id
      this.dialogResetPasswordFormVisible = true
    },
    resetPasswordOk() {
      // 直接调用表单实例的保存方法
      this.$refs.resetPasswordForm.save().then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
        // 关闭对话框
        this.dialogResetPasswordFormVisible = false
        this.refresh()
      })
    },
    resetPasswordCancel() {
      this.editId = -1
      this.$refs.resetPasswordForm.reset()
      this.dialogResetPasswordFormVisible = false
    }
  }
}
</script>
